package cn.iocoder.yudao.module.system.dal.mysql.erp.operatelog;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.system.controller.admin.erp.operatelog.vo.ErpOperateLogPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.operatelog.ErpOperateLogDO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.warehousein.WarehouseInDO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.warehouseout.WarehouseOutDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * erp操作日志 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ErpOperateLogMapper extends BaseMapperX<ErpOperateLogDO> {

    default PageResult<ErpOperateLogDO> selectPage(ErpOperateLogPageReqVO reqVO) {
        LambdaQueryWrapperX<ErpOperateLogDO> query=new LambdaQueryWrapperX<ErpOperateLogDO>()
                .eqIfPresent(ErpOperateLogDO::getUserId, reqVO.getUserId())
                .likeIfPresent(ErpOperateLogDO::getModule, reqVO.getModule())
                .likeIfPresent(ErpOperateLogDO::getDescription, reqVO.getDescription());
        String operate = reqVO.getOperate();
        if(StrUtil.isNotBlank(operate)){
            query.apply("(SELECT COUNT(0)>0 FROM system_users u WHERE u.id=t.customer_id and u.deleted=0 AND u.nickname LIKE '%"+operate+"%')");
        }
        LocalDate startTime=reqVO.getStartTime();LocalDate endTime=reqVO.getEndTime();
        if(ObjectUtil.isNotEmpty(startTime)&&ObjectUtil.isNotEmpty(endTime)){
            query.between(ErpOperateLogDO::getCreateTime,startTime.atStartOfDay(),endTime.atTime(23,59,59));
        }
        return selectPage(reqVO,query.orderByDesc(ErpOperateLogDO::getId));
    }

}